package controllers

import (
	"encoding/json"
	"github.com/yuchen16/ding/models"

	"fmt"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/httplib"
)

// Operations about object
type ObjectController struct {
	beego.Controller
}

// @Title Create
// @Description create object
// @Param	body		body 	models.Object	true		"The object content"
// @Success 200 {string} models.Object.Id
// @Failure 403 body is empty
// @router / [post]
func (o *ObjectController) Post() {
	fmt.Println(string(o.Ctx.Input.RequestBody))
	msg := models.MarkdownMsg{
		MsgType:  "markdown",
		Markdown: models.MarkdownContent{},
		At: models.AtInfo{
			IsAtAll: false,
		},
	}
	hookdata := new(models.HookCommitInfo)
	err := json.Unmarshal(o.Ctx.Input.RequestBody, hookdata)
	if err != nil {
		fmt.Println(err)
		o.ServeJSON()
	}
	fmt.Println(hookdata)
	cd := new(models.CommitDigest)
	cd.RepoName = hookdata.Repository.Name
	cd.Time = hookdata.Commits[0].Timestamp
	cd.Message = hookdata.Commits[0].Message
	cd.Author = hookdata.Commits[0].Author.Name
	cd.CommitId = hookdata.Commits[0].Id
	cd.Url = hookdata.Commits[0].Url

	msg.Markdown.Title = cd.RepoName
	msg.Markdown.Text = cd.Gen()
	hreq := httplib.Post(beego.AppConfig.String("robotCallback"))
	hreq.JSONBody(&msg)
	hreq.Header("Content-Type", "application/json")
	resp, err := hreq.String()
	if err != nil {
		fmt.Println("http request to dingding callback error: ", err)
	} else {
		fmt.Println("callback success! ", resp)
	}

	var ob models.Object
	json.Unmarshal(o.Ctx.Input.RequestBody, &ob)
	objectid := models.AddOne(ob)
	o.Data["json"] = map[string]string{"ObjectId": objectid}
	o.ServeJSON()
}

// @Title Get
// @Description find object by objectid
// @Param	objectId		path 	string	true		"the objectid you want to get"
// @Success 200 {object} models.Object
// @Failure 403 :objectId is empty
// @router /:objectId [get]
func (o *ObjectController) Get() {
	objectId := o.Ctx.Input.Param(":objectId")
	if objectId != "" {
		ob, err := models.GetOne(objectId)
		if err != nil {
			o.Data["json"] = err.Error()
		} else {
			o.Data["json"] = ob
		}
	}
	o.ServeJSON()
}

// @Title GetAll
// @Description get all objects
// @Success 200 {object} models.Object
// @Failure 403 :objectId is empty
// @router / [get]
func (o *ObjectController) GetAll() {
	obs := models.GetAll()
	o.Data["json"] = obs
	o.ServeJSON()
}

// @Title Update
// @Description update the object
// @Param	objectId		path 	string	true		"The objectid you want to update"
// @Param	body		body 	models.Object	true		"The body"
// @Success 200 {object} models.Object
// @Failure 403 :objectId is empty
// @router /:objectId [put]
func (o *ObjectController) Put() {
	objectId := o.Ctx.Input.Param(":objectId")
	var ob models.Object
	json.Unmarshal(o.Ctx.Input.RequestBody, &ob)

	err := models.Update(objectId, ob.Score)
	if err != nil {
		o.Data["json"] = err.Error()
	} else {
		o.Data["json"] = "update success!"
	}
	o.ServeJSON()
}

// @Title Delete
// @Description delete the object
// @Param	objectId		path 	string	true		"The objectId you want to delete"
// @Success 200 {string} delete success!
// @Failure 403 objectId is empty
// @router /:objectId [delete]
func (o *ObjectController) Delete() {
	objectId := o.Ctx.Input.Param(":objectId")
	models.Delete(objectId)
	o.Data["json"] = "delete success!"
	o.ServeJSON()
}
